package com.ssps.back.platform.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssps.back.platform.service.CouponService;
import com.ssps.entity.Coupon;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author li
 * @time 2020/07/23 15:55:54
 */

@Controller
@RequestMapping("coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    @RequestMapping("couponlist")
    public  String couponlist(){
        return "/coupon/couponList";
    }

    /*
     *
     * 信息展示
     *
     *
     */

    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object> couponFindAll(@RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize ){
        Map<String,Object> map=new HashMap<>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Coupon>couponList=couponService.couponFindAll();
            PageInfo<Coupon>pageInfo=new PageInfo<>(couponList);
            map.put("pageInfo",pageInfo);
            map.put("couponList",couponList);
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",false);
            e.printStackTrace();
        }
        return map;
    }



    /*
     *
     * 新增
     *
     *
     */

    @ResponseBody
    @RequestMapping("couponSave")
    public Map<String,Object> couponSave(Coupon coupon){
        Map<String,Object> map=new HashMap<>();
        try {
            couponService.couponSave(coupon);
            map.put("result",true);
        } catch (Exception e) {
            map.put("result",false);
            e.printStackTrace();
        }

        return  map;

    }
}
